/*     */ package dm.jdbc.pool;
/*     */ 
/*     */ import dm.jdbc.dbaccess.Const;
/*     */ import dm.jdbc.dbaccess.DBError;
/*     */ import java.io.Serializable;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Hashtable;
/*     */ import java.util.ResourceBundle;
/*     */ import java.util.Stack;
/*     */ import javax.naming.NamingException;
/*     */ import javax.naming.Reference;
/*     */ import javax.naming.Referenceable;
/*     */ import javax.naming.StringRefAddr;
/*     */ import javax.sql.ConnectionPoolDataSource;
/*     */ import javax.sql.PooledConnection;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DmdbConnectionCache_bs
/*     */   extends DmdbDataSource_bs
/*     */   implements Serializable, Referenceable
/*     */ {
/*     */   private static final long serialVersionUID = -7503863927581289230L;
/*     */   private ConnectionPoolDataSource connectionPoolDataSource;
/*     */   private static final int DEFAULT_MIN_LIMIT = 0;
/*     */   private static final int DEFAULT_MAX_LIMIT = 10;
/*     */   private int MIN_LIMIT;
/*     */   private int MAX_LIMIT;
/*     */   private Stack cache;
/*     */   private Hashtable activeCache;
/*     */   private int cacheSize;
/*     */   private int activeSize;
/*     */   private int cacheScheme;
/*     */   public static final int DYNAMIC_SCHEME = 1;
/*     */   public static final int FIXED_WAIT_SCHEME = 2;
/*     */   public static final int FIXED_RETURN_NULL_SCHEME = 3;
/*     */   private DmdbConnectionEventListener_bs connectionEventListener;
/*     */   
/*     */   public DmdbConnectionCache_bs()
/*     */     throws SQLException
/*     */   {
/* 120 */     this(null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public DmdbConnectionCache_bs(ConnectionPoolDataSource connectionpooldatasource)
/*     */     throws SQLException
/*     */   {
/* 133 */     this.MIN_LIMIT = 0;
/* 134 */     this.MAX_LIMIT = 10;
/* 135 */     this.cacheSize = 0;
/* 136 */     this.activeSize = 0;
/* 137 */     this.cacheScheme = 1;
/* 138 */     this.cache = new Stack();
/* 139 */     this.activeCache = new Hashtable();
/* 140 */     this.connectionPoolDataSource = connectionpooldatasource;
/* 141 */     this.connectionEventListener = new DmdbConnectionEventListener_bs(this);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void checkCredentials(String user, String password)
/*     */     throws SQLException
/*     */   {
/* 155 */     String l_user = null;
/* 156 */     String l_password = null;
/* 157 */     if (this.connectionPoolDataSource != null) {
/* 158 */       l_user = 
/* 159 */         ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource).getUser();
/* 160 */       l_password = ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource)
/* 161 */         .getPassword();
/*     */     }
/*     */     
/* 164 */     if (((user != null) && (!user.equals(l_user))) || ((password != null) && 
/* 165 */       (!password.equals(l_password)))) {
/* 166 */       throw new SQLException("Cannot create a connection with user as " + 
/* 167 */         user + " as it is doesnt match the existing user " + 
/* 168 */         this.user + " Or the Password");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void close()
/*     */     throws SQLException
/*     */   {
/* 179 */     Enumeration enumeration = this.activeCache.keys();
/* 180 */     DmdbPooledConnection_bs dmpooledconnection; for (; enumeration.hasMoreElements(); reusePooledConnection(dmpooledconnection)) {
/* 181 */       DmdbPooledConnection_bs dmpooledconnection1 = (DmdbPooledConnection_bs)enumeration
/* 182 */         .nextElement();
/* 183 */       dmpooledconnection = (DmdbPooledConnection_bs)this.activeCache
/* 184 */         .get(dmpooledconnection1);
/*     */     }
/*     */     
/* 187 */     while (!this.cache.empty()) { closeSingleConnection(
/* 188 */         (DmdbPooledConnection_bs)this.cache.pop());
/*     */     }
/*     */     
/* 191 */     this.cache = null;
/* 192 */     this.activeCache = null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void closePooledConnection(PooledConnection pooledconnection)
/*     */     throws SQLException
/*     */   {
/* 204 */     detachSingleConnection(pooledconnection);
/* 205 */     closeSingleConnection(pooledconnection);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void closeSingleConnection(PooledConnection pooledconnection)
/*     */     throws SQLException
/*     */   {
/* 214 */     this.cacheSize -= 1;
/* 215 */     pooledconnection.close();
/* 216 */     pooledconnection = null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void detachSingleConnection(PooledConnection pooledconnection)
/*     */   {
/* 226 */     this.activeSize -= 1;
/* 227 */     pooledconnection.removeConnectionEventListener(this.connectionEventListener);
/* 228 */     this.activeCache.remove(pooledconnection);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getActiveSize()
/*     */   {
/* 237 */     return this.activeSize;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized int getCacheScheme()
/*     */   {
/* 246 */     return this.cacheScheme;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getCacheSize()
/*     */   {
/* 255 */     return this.cacheSize;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized Connection getConnection()
/*     */     throws SQLException
/*     */   {
/* 265 */     return getConnection(this.user, this.password);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized Connection getConnection(String user, String password)
/*     */     throws SQLException
/*     */   {
/*     */     DmdbPooledConnection_bs dmpooledconnection;
/*     */     
/*     */ 
/*     */ 
/*     */     DmdbPooledConnection_bs dmpooledconnection;
/*     */     
/*     */ 
/* 281 */     if (!this.cache.empty()) {
/* 282 */       checkCredentials(user, password);
/* 283 */       dmpooledconnection = (DmdbPooledConnection_bs)this.cache.pop();
/* 284 */     } else if ((this.cacheSize < this.MAX_LIMIT) || (this.cacheScheme == 1)) {
/* 285 */       String l_user = null;
/* 286 */       String l_password = null;
/* 287 */       if (this.connectionPoolDataSource != null) {
/* 288 */         l_user = 
/* 289 */           ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource).getUser();
/* 290 */         l_password = ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource)
/* 291 */           .getPassword();
/*     */       }
/* 293 */       if ((this.cacheSize > 0) && (user != null) && (!user.equals(l_user))) {
/* 294 */         DBError.throwSQLException(
/* 295 */           Const.res.getString("error.username.differ"));
/* 296 */       } else if (user != null) {
/* 297 */         this.user = user;
/* 298 */         if (this.connectionPoolDataSource != null)
/*     */         {
/* 300 */           ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource).setUser(user);
/*     */         }
/*     */       }
/*     */       
/* 304 */       if ((this.cacheSize > 0) && (password != null) && 
/* 305 */         (!password.equals(l_password))) {
/* 306 */         DBError.throwSQLException(
/* 307 */           Const.res.getString("error.password.differ"));
/* 308 */       } else if (password != null) {
/* 309 */         this.password = password;
/* 310 */         if (this.connectionPoolDataSource != null)
/*     */         {
/* 312 */           ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource).setPassword(password);
/*     */         }
/*     */       }
/*     */       
/* 316 */       DmdbPooledConnection_bs dmpooledconnection = getNewPoolConnection();
/* 317 */       this.cacheSize += 1;
/*     */     } else {
/* 319 */       if (this.cacheScheme == 3) {
/* 320 */         return null;
/*     */       }
/* 322 */       checkCredentials(user, password);
/*     */       
/* 324 */       while (this.cache.empty()) {
/* 325 */         synchronized (this.cache) {
/*     */           try {
/* 327 */             this.cache.wait(1000L);
/*     */           }
/*     */           catch (InterruptedException localInterruptedException) {}
/*     */         }
/*     */       }
/* 332 */       dmpooledconnection = (DmdbPooledConnection_bs)this.cache.pop();
/*     */     }
/*     */     
/* 335 */     if (dmpooledconnection != null)
/*     */     {
/* 337 */       dmpooledconnection.addConnectionEventListener(this.connectionEventListener);
/* 338 */       this.activeCache.put(dmpooledconnection, dmpooledconnection);
/* 339 */       this.activeSize += 1;
/* 340 */       return dmpooledconnection.getConnection();
/*     */     }
/* 342 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized int getMaxLimit()
/*     */   {
/* 352 */     return this.MAX_LIMIT;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized int getMinLimit()
/*     */   {
/* 361 */     return this.MIN_LIMIT;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private synchronized DmdbPooledConnection_bs getNewPoolConnection()
/*     */     throws SQLException
/*     */   {
/* 372 */     if (this.connectionPoolDataSource == null) {
/* 373 */       if ((this.user == null) || (this.password == null)) {
/* 374 */         DBError.throwSQLException(
/* 375 */           Const.res.getString("error.UserOrPwd.null"));
/*     */       }
/* 377 */       this.connectionPoolDataSource = new DmdbConnectionPoolDataSource_bs();
/*     */       
/* 379 */       ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource)
/* 380 */         .setUser(this.user);
/* 381 */       ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource)
/* 382 */         .setPassword(this.password);
/* 383 */       ((DmdbConnectionPoolDataSource_bs)this.connectionPoolDataSource)
/* 384 */         .setURL(super.getURL());
/*     */     }
/* 386 */     return (DmdbPooledConnection_bs)this.connectionPoolDataSource
/* 387 */       .getPooledConnection();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Reference getReference()
/*     */     throws NamingException
/*     */   {
/* 397 */     String factoryName = "dm.jdbc.pool.DmdbDataSourceFactory_bs";
/* 398 */     Reference ref = new Reference(getClass().getName(), factoryName, null);
/* 399 */     ref.add(new StringRefAddr("url", getURL()));
/* 400 */     ref.add(new StringRefAddr("server", getServer()));
/* 401 */     ref.add(new StringRefAddr("user", getUser()));
/* 402 */     ref.add(new StringRefAddr("password", this.password));
/* 403 */     ref.add(new StringRefAddr("database", getDatabase()));
/* 404 */     ref.add(new StringRefAddr("minLimit", Integer.toString(this.MIN_LIMIT)));
/* 405 */     ref.add(new StringRefAddr("maxLimit", Integer.toString(this.MAX_LIMIT)));
/* 406 */     ref.add(new StringRefAddr("cacheScheme", Integer.toString(this.cacheScheme)));
/* 407 */     return ref;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void reusePooledConnection(PooledConnection pooledconnection)
/*     */     throws SQLException
/*     */   {
/* 419 */     detachSingleConnection(pooledconnection);
/*     */     
/* 421 */     if ((this.cacheSize > this.MAX_LIMIT) && (this.cacheScheme == 1)) {
/* 422 */       closeSingleConnection(pooledconnection);
/*     */     } else {
/* 424 */       this.cache.push(pooledconnection);
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void setCacheScheme(int i) throws SQLException {
/* 429 */     if ((i == 1) || (i == 3) || 
/* 430 */       (i == 2)) {
/* 431 */       this.cacheScheme = i;
/*     */     } else {
/* 433 */       DBError.throwSQLException(
/* 434 */         Const.res.getString("error.pooled.inexistence"));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void setConnectionPoolDataSource(ConnectionPoolDataSource connectionpooldatasource)
/*     */     throws SQLException
/*     */   {
/* 448 */     if (this.cacheSize > 0) {
/* 449 */       DBError.throwSQLException(
/* 450 */         Const.res.getString("error.cachesize.overload"));
/*     */     }
/*     */     
/* 453 */     this.connectionPoolDataSource = connectionpooldatasource;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void setMaxLimit(int i)
/*     */     throws SQLException
/*     */   {
/* 464 */     if ((i < 0) || (i < this.MIN_LIMIT)) {
/* 465 */       DBError.throwSQLException(Const.res.getString("error.pooled.limit"));
/*     */     }
/*     */     
/* 468 */     this.MAX_LIMIT = i;
/* 469 */     if ((this.cacheSize > this.MAX_LIMIT) && (this.cacheScheme != 1)) {
/* 470 */       for (int j = this.MAX_LIMIT; j < this.cacheSize; j++) {
/* 471 */         if (this.cache.empty()) {
/* 472 */           DBError.throwSQLException(
/* 473 */             Const.res.getString("error.pooled.overflow"));
/*     */         } else {
/* 475 */           ((DmdbPooledConnection_bs)this.cache.pop()).close();
/*     */         }
/*     */       }
/*     */       
/* 479 */       this.cacheSize = this.MAX_LIMIT;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void setMinLimit(int i)
/*     */     throws SQLException
/*     */   {
/* 491 */     if ((i < 0) || (i > this.MAX_LIMIT)) {
/* 492 */       DBError.throwSQLException(
/* 493 */         Const.res.getString("error.pooled.minOverflow"));
/*     */     }
/* 495 */     this.MIN_LIMIT = i;
/* 496 */     if (this.cacheSize < this.MIN_LIMIT) {
/* 497 */       for (int j = this.cacheSize; j < this.MIN_LIMIT; j++) {
/* 498 */         DmdbPooledConnection_bs dmpooledconnection = getNewPoolConnection();
/* 499 */         this.cache.push(dmpooledconnection);
/*     */       }
/*     */       
/* 502 */       this.cacheSize = this.MIN_LIMIT;
/*     */     }
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\pool\DmdbConnectionCache_bs.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */